/**
 * Created by isoft-work 5 on 12/8/2014.
 */
$(document).ready(function(){
//   render
   lstProduct = null;
   _filterPrice = null;
   _textFilterPrice = null;
   _filterSize = null;
   renderProduct();
});

function renderProduct() {
   getListProduct();
   getCatalogProduct();
}

function getListProduct() {
   var _gender = ISS.SESSION.getSession('session_gender');
   var _maDMSP = ISS.SESSION.getSession('session_maDMSP');

   ISS.CONTROL.lockScreen('#content');
   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/product/getProductByIdDMSP/' + _gender + '/' + _maDMSP,
      type: 'GET',
      success: function (data) {
         var responseInfo = data.responseObject;
         lstProduct = responseInfo;
         getListProductComplete(responseInfo);
         ISS.CONTROL.unLockScreen('#content');
      }
   });
}

function getListProductComplete(listProduct) {
   // show list product
   var product = '';
   for(var i = 0; i < listProduct.length; i++) {
      var size = '', cost, image;
      var _chiTietSanPhams = listProduct[i].chiTietSanPhams,
         _listSize = listProduct[i].size;
      for(var s = 0; s < _listSize.length; s++) {
         size += ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailSize,{
            tenSize: _listSize[s].tenSize
         })
      }
      for(var c = 0; c < _chiTietSanPhams.length; c++) {
         if(_chiTietSanPhams[c].chiTietKhuyenMai.length > 0) {
            // check date sale off
            var _ngayBD = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayBD;
            var _ngayKT = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayKT;
            if(ISS.DATE.isBetWeen(_ngayBD, _ngayKT)) {
               cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                  cost: _chiTietSanPhams[c].giaSP
               });
               image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                  image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[c].hinhAnhSanPham[0].tenHinh,
                  off: _chiTietSanPhams[c].chiTietKhuyenMai[0].phanTram
               });
               break;
            }
            else {
               if(c == _chiTietSanPhams.length - 1) {
                  cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                     cost: _chiTietSanPhams[0].giaSP
                  });
                  if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                     image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                        image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                        off: '0'
                     });
                  }
                  else {
                     image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                        image: ISS.SESSION.getSession('contextPath'),
                        off: '0'
                     });
                  }
               }
            }
         }
         else {
            if(c == _chiTietSanPhams.length - 1) {
               cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                  cost: _chiTietSanPhams[0].giaSP
               });
               if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                  image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                     image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                     off: '0'
                  });
               }
               else {
                  image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                     image: ISS.SESSION.getSession('contextPath'),
                     off: '0'
                  });
               }
            }
         }
      }
      product += ISS.STRING_UTIL.format(TEMPLATE.Product.item,{
         itemDetailSize: size,
         image: image,
         tenSP: listProduct[i].tenSP,
         thongTin: listProduct[i].thongTin,
         cost: cost,
         detailPage: ISS.SESSION.getSession('contextPath') + '/service/product/toPageProductDetail/' + lstProduct[i].maSP
      });
   }
   ISS.DOM.html('#productsCatalog', product);
   ISS.DOM.paging('#catalog_container');
   ISS.DOM.scroll();
}

function getCatalogProduct() {
   var _maDMSP = ISS.SESSION.getSession('session_maDMSP');
   var _gender = ISS.SESSION.getSession('session_gender');
   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/product/getCategoryMenu/' + _maDMSP + '/' +_gender,
      type: 'GET',
      success: function (data) {
         var responseInfo = data.responseObject;
         getCatalogProductComplete(responseInfo);
         renderCatalogSize(responseInfo.kichThuoc);
      }
   });
}

function getCatalogProductComplete(catalog) {
   var _loaiDanhMucSanPham = catalog.loaiDanhMucSanPham;
   var catalogItem = '', _catalog;
   for(var c = 0; c < _loaiDanhMucSanPham.length; c++) {
      var _href = ISS.SESSION.getSession('contextPath') + '/service/product/toPageProduct/'+ ISS.SESSION.getSession('session_gender') + '/' + _loaiDanhMucSanPham[c].maLoaiDMSP;
      if(_loaiDanhMucSanPham[c].maLoaiDMSP == ISS.SESSION.getSession('session_maDMSP')){
         catalogItem += ISS.STRING_UTIL.format(TEMPLATE.Product.catalogItem,{
            danhMucSP: _loaiDanhMucSanPham[c].tenLoaiDMSP,
            selected:'selected',
            href: _href
         });
      }
      else {
         catalogItem += ISS.STRING_UTIL.format(TEMPLATE.Product.catalogItem,{
            danhMucSP: _loaiDanhMucSanPham[c].tenLoaiDMSP,
            selected:'' ,
            href: _href
         });
      }
   }
   _catalog = ISS.STRING_UTIL.format(TEMPLATE.Product.catalog,{
      catalogItem: catalogItem,
      danhMuc: catalog.tenLoai
   });
   ISS.DOM.html('#filter_category', _catalog);
   renderBread(_loaiDanhMucSanPham);
}

function renderBread(loaiDanhMucSanPham) {
   var _gender = ISS.SESSION.getSession('session_gender');
   var _maDMSP = ISS.SESSION.getSession('session_maDMSP');

   for(var c = 0; c < loaiDanhMucSanPham.length; c++) {
      if(_maDMSP == loaiDanhMucSanPham[c].maLoaiDMSP) {
         if(_gender == 'NAM') {
            var _bread = ISS.STRING_UTIL.format(TEMPLATE.Product.breadMen,{
               danhMucSP: loaiDanhMucSanPham[c].tenLoaiDMSP,
               shopPage: ISS.SESSION.getSession('contextPath'),
               menPage: ISS.SESSION.getSession('contextPath') + '/jsp/user/user-men.jsp'
            });
         }
         else {
            _bread = ISS.STRING_UTIL.format(TEMPLATE.Product.breadWomen,{
               danhMucSP: loaiDanhMucSanPham[c].tenLoaiDMSP,
               shopPage: ISS.SESSION.getSession('contextPath'),
               womenPage: ISS.SESSION.getSession('contextPath') + '/jsp/user/user-women.jsp'
            });
         }
         break;
      }
   }
   ISS.DOM.html('#breadcrumb', _bread);
}

function renderCatalogSize(kichThuoc) {
   var catalogSize = '';
  for(var i = 0; i < kichThuoc.length; i++) {
     catalogSize += ISS.STRING_UTIL.format(TEMPLATE.Product.catalogSize,{
        tenKichThuoc: kichThuoc[i].tenSize,
        id: kichThuoc[i].maSize,
        value: kichThuoc[i].tenSize
     });
  }
   ISS.DOM.html('#filter_price', catalogSize);
   bindEventProduct(kichThuoc);
}

function bindEventProduct(kichThuoc) {
   // event filter size
   for(var i = 0; i < kichThuoc.length; i++) {
      ISS.EVENT.bindEvent('#' + kichThuoc[i].maSize, 'click', function () {
         _filterSize = ISS.DOM.getText('#' + this.id);
         filterProductSize(_filterSize);
      });
   }
   // event filter price
   for(i = 0; i < 6; i++) {
      ISS.EVENT.bindEvent('#filter_price_' + i, 'click', function () {
         var values = ISS.DOM.getData(this,'cost');
         _filterPrice = values;
         _textFilterPrice = this.textContent;
         filterProductPrice(values, _textFilterPrice);
      });
   }

   // event remove filter
   ISS.EVENT.bindEvent('#remove_filter_size', 'click', function () {
      removeFilterSize();
   });

   ISS.EVENT.bindEvent('#remove_filter_price', 'click', function () {
      removeFilterPrice();
   });
}

function removeFilterSize() {
   _filterSize = null;

   ISS.DOM.hide('#facet_size_system');
   if(_textFilterPrice) {
      var beginCost = _filterPrice.substring(0,_filterPrice.indexOf('-')),
         endCost = _filterPrice.substring(_filterPrice.indexOf('-') + 1, _filterPrice.length);
      showProductAgainFilterPrice(beginCost, endCost);
   }
   else {
      getListProductComplete(lstProduct);
   }
}

function removeFilterPrice() {
   _filterPrice = null;
   _textFilterPrice = null;

   ISS.DOM.hide('#facet_price_system');
   if(_filterSize) {
      showProductAgainFilterSize(_filterSize);
   }
   else {
      getListProductComplete(lstProduct);
   }


}

function filterProductPrice(values, text) {
   var beginCost = values.substring(0,values.indexOf('-')),
      endCost = values.substring(values.indexOf('-') + 1, values.length);
   if(_filterSize) {
      showProductAgainFilterSizeAndPrice(_filterSize, beginCost, endCost);
   }
   else {
      showProductAgainFilterPrice(beginCost, endCost);
   }

   ISS.DOM.text('#name_price_selected', _textFilterPrice);
   ISS.DOM.show('#facet_price_system');
}

function filterProductSize(filterSize) {
   if(_textFilterPrice) {
      var beginCost = _filterPrice.substring(0, _filterPrice.indexOf('-')),
         endCost = _filterPrice.substring(_filterPrice.indexOf('-') + 1, _filterPrice.length);
      showProductAgainFilterSizeAndPrice(_filterSize, beginCost, endCost);
   }
   else {
      showProductAgainFilterSize(filterSize);
   }

   ISS.DOM.text('#name_size_selected', _filterSize);
   ISS.DOM.show('#facet_size_system');
}

function showProductAgainFilterSize(maSize) {
   var product = '';
   for(var i = 0; i < lstProduct.length; i++) {
      var size = '', cost, image ;
      var _chiTietSanPhams = lstProduct[i].chiTietSanPhams,
         _listSize = lstProduct[i].size;
      for(var t = 0; t < _listSize.length; t++) {
         if(_listSize[t].tenSize == maSize) {
            for(var s = 0; s < _listSize.length; s++) {
               size += ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailSize,{
                  tenSize: _listSize[s].tenSize
               })
            }
            for(var c = 0; c < _chiTietSanPhams.length; c++) {
               if(_chiTietSanPhams[c].chiTietKhuyenMai.length > 0) {
                  // check date sale off
                  var _ngayBD = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayBD;
                  var _ngayKT = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayKT;
                  if(ISS.DATE.isBetWeen(_ngayBD, _ngayKT)) {
                     cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                        cost: _chiTietSanPhams[c].giaSP
                     });
                     image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                        image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[c].hinhAnhSanPham[0].tenHinh,
                        off: _chiTietSanPhams[c].chiTietKhuyenMai[0].phanTram
                     });
                     break;
                  }
                  else {
                     if(c == _chiTietSanPhams.length - 1) {
                        cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                           cost: _chiTietSanPhams[0].giaSP
                        });
                        if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                           image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                              image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                              off: '0'
                           });
                        }
                        else {
                           image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                              image: ISS.SESSION.getSession('contextPath'),
                              off: '0'
                           });
                        }
                     }
                  }
               }
               else {
                  if(c == _chiTietSanPhams.length - 1) {
                     cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                        cost: _chiTietSanPhams[0].giaSP
                     });
                     if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                        image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                           image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                           off: '0'
                        });
                     }
                     else {
                        image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                           image: ISS.SESSION.getSession('contextPath'),
                           off: '0'
                        });
                     }
                  }
               }
            }
            product += ISS.STRING_UTIL.format(TEMPLATE.Product.item,{
               itemDetailSize: size,
               image: image,
               tenSP: lstProduct[i].tenSP,
               thongTin: lstProduct[i].thongTin,
               cost: cost,
               detailPage: ISS.SESSION.getSession('contextPath') + '/service/product/toPageProductDetail/' + lstProduct[i].maSP
            });
            break;
         }
      }
   }
   ISS.DOM.html('#productsCatalog', product);
   ISS.DOM.paging('#catalog_container');
}

function showProductAgainFilterPrice(begin, end) {
   var product = '';
   for(var i = 0; i < lstProduct.length; i++) {
      var size = '', cost, image;
      var _chiTietSanPhams = lstProduct[i].chiTietSanPhams,
         _listSize = lstProduct[i].size;
      for(var t = 0; t < _chiTietSanPhams.length; t++) {
         if(_chiTietSanPhams[t].giaSP >= begin && _chiTietSanPhams[t].giaSP <= end) {
            for(var s = 0; s < _listSize.length; s++) {
               size += ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailSize,{
                  tenSize: _listSize[s].tenSize
               })
            }
            for(var c = 0; c < _chiTietSanPhams.length; c++) {
               if(_chiTietSanPhams[c].chiTietKhuyenMai.length > 0) {
                  // check date sale off
                  var _ngayBD = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayBD;
                  var _ngayKT = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayKT;
                  if(ISS.DATE.isBetWeen(_ngayBD, _ngayKT)) {
                     cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                        cost: _chiTietSanPhams[c].giaSP
                     });
                     image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                        image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[c].hinhAnhSanPham[0].tenHinh,
                        off: _chiTietSanPhams[c].chiTietKhuyenMai[0].phanTram
                     });
                     break;
                  }
                  else {
                     if(c == _chiTietSanPhams.length - 1) {
                        cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                           cost: _chiTietSanPhams[0].giaSP
                        });
                        if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                           image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                              image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                              off: '0'
                           });
                        }
                        else {
                           image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                              image: ISS.SESSION.getSession('contextPath'),
                              off: '0'
                           });
                        }
                     }
                  }
               }
               else {
                  if(c == _chiTietSanPhams.length - 1) {
                     cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                        cost: _chiTietSanPhams[0].giaSP
                     });
                     if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                        image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                           image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                           off: '0'
                        });
                     }
                     else {
                        image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                           image: ISS.SESSION.getSession('contextPath'),
                           off: '0'
                        });
                     }
                  }
               }
            }
            product += ISS.STRING_UTIL.format(TEMPLATE.Product.item,{
               itemDetailSize: size,
               image: image,
               tenSP: lstProduct[i].tenSP,
               thongTin: lstProduct[i].thongTin,
               cost: cost,
               detailPage: ISS.SESSION.getSession('contextPath') + '/service/product/toPageProductDetail/' + lstProduct[i].maSP
            });
            break;
         }
      }
   }
   ISS.DOM.html('#productsCatalog', product);
   ISS.DOM.paging('#catalog_container');
}

function showProductAgainFilterSizeAndPrice(maSize, begin, end) {
   var product = '';
   for(var i = 0; i < lstProduct.length; i++) {
      var size = '', cost, image ;
      var _chiTietSanPhams = lstProduct[i].chiTietSanPhams,
         _listSize = lstProduct[i].size;
      if(checkSizeAndPrice(_chiTietSanPhams, _listSize, maSize, begin, end)) {
         for(var s = 0; s < _listSize.length; s++) {
            size += ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailSize,{
               tenSize: _listSize[s].tenSize
            })
         }
         for(var c = 0; c < _chiTietSanPhams.length; c++) {
            if(_chiTietSanPhams[c].chiTietKhuyenMai.length > 0) {
               // check date sale off
               var _ngayBD = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayBD;
               var _ngayKT = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayKT;
               if(ISS.DATE.isBetWeen(_ngayBD, _ngayKT)) {
                  cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                     cost: _chiTietSanPhams[c].giaSP
                  });
                  image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                     image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[c].hinhAnhSanPham[0].tenHinh,
                     off: _chiTietSanPhams[c].chiTietKhuyenMai[0].phanTram
                  });
                  break;
               }
               else {
                  if(c == _chiTietSanPhams.length - 1) {
                     cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                        cost: _chiTietSanPhams[0].giaSP
                     });
                     if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                        image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                           image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                           off: '0'
                        });
                     }
                     else {
                        image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                           image: ISS.SESSION.getSession('contextPath'),
                           off: '0'
                        });
                     }
                  }
               }
            }
            else {
               if(c == _chiTietSanPhams.length - 1) {
                  cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                     cost: _chiTietSanPhams[0].giaSP
                  });
                  if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                     image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                        image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                        off: '0'
                     });
                  }
                  else {
                     image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                        image: ISS.SESSION.getSession('contextPath'),
                        off: '0'
                     });
                  }
               }
            }
         }
         product += ISS.STRING_UTIL.format(TEMPLATE.Product.item,{
            itemDetailSize: size,
            image: image,
            tenSP: lstProduct[i].tenSP,
            thongTin: lstProduct[i].thongTin,
            cost: cost,
            detailPage: ISS.SESSION.getSession('contextPath') + '/service/product/toPageProductDetail/' + lstProduct[i].maSP
         });
      }
   }
   ISS.DOM.html('#productsCatalog', product);
   ISS.DOM.paging('#catalog_container');
}

function checkSizeAndPrice(chiTietSanPhams, listSize, maSize, begin, end) {
   for (var s = 0; s < listSize.length; s++) {
      if(listSize[s].tenSize == maSize) {
         for(var t = 0; t < chiTietSanPhams.length; t++) {
            if (chiTietSanPhams[t].giaSP >= begin && chiTietSanPhams[t].giaSP <= end) {
               return true;
            }
         }
      }
      else {
         if(s == listSize.length - 1) {
            return false;
         }
      }
   }
}
